FROM python:3.10-slim

# 设置工作目录
WORKDIR /app

# 设置环境变量
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV TZ=Asia/Shanghai

# 更换为清华Debian源（正确路径）
RUN sed -i 's|deb.debian.org/debian|mirrors.tuna.tsinghua.edu.cn/debian|g' /etc/apt/sources.list && \
    sed -i 's|security.debian.org/debian-security|mirrors.tuna.tsinghua.edu.cn/debian-security|g' /etc/apt/sources.list

# 安装系统依赖
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        default-libmysqlclient-dev \
        pkg-config \
        gcc \
    	libgl1-mesa-glx \
        libglib2.0-0 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 安装 Python 依赖
COPY requirements.txt /app/requirements.txt
RUN pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

RUN pip install --no-cache-dir -r requirements.txt

# 创建简化的启动脚本
RUN echo '#!/bin/bash\n\
set -e\n\
\n\
# 启动 Gunicorn\n\
echo "启动 Gunicorn..."\n\
exec gunicorn core.wsgi:application --bind 0.0.0.0:8000 --workers 4 --timeout 120 --log-level=info\n'\
> /start.sh

RUN chmod +x /start.sh

# 暴露端口
EXPOSE 8000

# 启动应用
CMD ["/start.sh"]